package Tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    private Socket clientSocket=null;
    public TcpClient(String serverIP,int port) throws IOException {
        clientSocket=new Socket(serverIP,port);
    }
    public void start()
    {
        Scanner scaner=new Scanner(System.in);
        try(InputStream inputStream=clientSocket.getInputStream())
        {
            OutputStream outputStream=clientSocket.getOutputStream();
            PrintWriter printWriter=new PrintWriter(outputStream);
            Scanner scannerNetwork=new Scanner(inputStream);
            while(true)
            {
                //从控制台获取用户输入的内容
                System.out.println("-> ");
                String request=scaner.next();
                //把字符串作为请求，发送给服务器

                printWriter.println(request);
                printWriter.flush();

                //读取服务器返回的响应
                String response=scannerNetwork.next();
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpClient tcpClient=new TcpClient("127.0.0.1",9090);
        tcpClient.start();
    }
}
